package com.atguigu.gmall.pms.service.impl;

import com.atguigu.gmall.pms.entity.AttrEntity;
import com.atguigu.gmall.pms.entity.SkuAttrValueEntity;
import com.atguigu.gmall.pms.entity.SpuAttrValueEntity;
import com.atguigu.gmall.pms.service.AttrService;
import com.atguigu.gmall.pms.service.SkuAttrValueService;
import com.atguigu.gmall.pms.service.SpuAttrValueService;
import com.atguigu.gmall.pms.vo.AttrGroupVo;
import com.atguigu.gmall.pms.vo.GroupAttrValueVo;
import com.atguigu.gmall.pms.vo.ItemGroupVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.AttrGroupMapper;
import com.atguigu.gmall.pms.entity.AttrGroupEntity;
import com.atguigu.gmall.pms.service.AttrGroupService;
import org.springframework.util.CollectionUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    AttrService attrService;
    @Autowired
    SkuAttrValueService skuAttrValueService;
    @Autowired
    SpuAttrValueService spuAttrValueService;
    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<AttrGroupEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public List<AttrGroupVo> queryAttrgroupWithAttrsByCid(String categoryId) {
        /*
         使用categoryId先查询attr_group表中查询 属性分组集合

          遍历属性分组集合，使用每个属性分组的id为他查询 自己的属性集合 并绑定
         */
        List<AttrGroupEntity> attrGroupEntities = this.list(new LambdaQueryWrapper<AttrGroupEntity>()
                .eq(AttrGroupEntity::getCategoryId, categoryId));
        List<AttrEntity> attrEntities = attrService.list(new LambdaQueryWrapper<AttrEntity>()
                .eq(AttrEntity::getType, 1));
        //遍历所有的属性分组，为他挑选 属性集合  并封装为AttrGroupVo对象 最终存入到AttrGroupVo的集合中
        List<AttrGroupVo> attrGroupVos = attrGroupEntities.stream()
                .map(attrGroupEntity -> {
                    AttrGroupVo attrGroupVo = new AttrGroupVo();
                    BeanUtils.copyProperties(attrGroupEntity,attrGroupVo);
                    //使用分组对象的id 到属性对象集合中匹配 那个属性对象的groupId等于当前分组id
                    attrGroupVo.setAttrEntities(attrEntities.stream().filter(attrEntity ->
                            attrEntity.getGroupId()==attrGroupEntity.getId())
                            .collect(Collectors.toList()));
                    return attrGroupVo;
                }).collect(Collectors.toList());
//                .map() 将集合中的每个元素遍历转为指定的对象返回
//                .filter() 对集合中的数据进行遍历时 过滤每一个对象
//                .collect(Collectors.toList()) 将上一个方法返回的对象转为list集合

//        List<AttrGroupVo> attrGroupVos = new ArrayList<>();
//        //遍历查询每个分组下的属性集合 所有查询的结果集 其实就是  attr表中的type=1的所有的数据
//        attrGroupEntities.forEach(attrGroupEntity -> {
//            AttrGroupVo attrGroupVo = new AttrGroupVo();
//        BeanUtils.copyProperties(attrGroupEntity , attrGroupVo);
//        List<AttrEntity> attrEntities = attrService.list(new LambdaQueryWrapper<AttrEntity>()
//                .eq(AttrEntity::getGroupId, attrGroupEntity.getId())
//        .eq(AttrEntity::getType , 1));
//        attrGroupVo.setAttrEntities(attrEntities);
//        attrGroupVos.add(attrGroupVo);
//
//    });

        return attrGroupVos;
    }
    @Override
    public List<ItemGroupVo> queryGroupAndAttrValues(String c3id, String spuId, String skuId) {
        //1、查询c3id下的所有的属性分组
        List<AttrGroupEntity> attrGroupEntities = this.list(new LambdaQueryWrapper<AttrGroupEntity>()
                .eq(AttrGroupEntity::getCategoryId, c3id));
        if(CollectionUtils.isEmpty(attrGroupEntities)){
            return null;
        }
        //2、查询每个属性分组下的所有的属性
        //一个属性分组 对应一个ItemGroupVo对象
        List<ItemGroupVo> itemGroupVos = attrGroupEntities.stream().map(attrGroupEntity -> {
            ItemGroupVo itemGroupVo = new ItemGroupVo();
            itemGroupVo.setGroupName(attrGroupEntity.getName());
            itemGroupVo.setGroupId(attrGroupEntity.getId().toString());
            List<AttrEntity> attrEntities = attrService.list(new LambdaQueryWrapper<AttrEntity>()
                    .eq(AttrEntity::getGroupId, attrGroupEntity.getId()));
            if (CollectionUtils.isEmpty(attrEntities)) {
                return itemGroupVo;
            }
            //正在遍历的组内的属性集合：attrEntities  ==》List<GroupAttrValueVo> attrs;
            //遍历将attrEntities 封装为GroupAttrValueVo的集合
            //获取attrId的集合
            List<Long> attrIds = attrEntities.stream().map(attrEntity -> attrEntity.getId())
                    .collect(Collectors.toList());
            List<SpuAttrValueEntity> spuAttrValueEntities = spuAttrValueService.list(new LambdaQueryWrapper<SpuAttrValueEntity>()
                    .eq(SpuAttrValueEntity::getSpuId, spuId)
                    .in(SpuAttrValueEntity::getAttrId, attrIds));

            List<SkuAttrValueEntity> skuAttrValueEntities = skuAttrValueService.list(new LambdaQueryWrapper<SkuAttrValueEntity>()
                    .eq(SkuAttrValueEntity::getSkuId, skuId)
                    .in(SkuAttrValueEntity::getAttrId, attrIds));
            List<GroupAttrValueVo> allGroupAttrValuesVos = new ArrayList<>();
            if (!CollectionUtils.isEmpty(spuAttrValueEntities)) {
                List<GroupAttrValueVo> groupAttrValueVos = spuAttrValueEntities.stream().map(spuAttrValueEntity -> {
                    GroupAttrValueVo groupAttrValueVo = new GroupAttrValueVo();
                    groupAttrValueVo.setAttrId(spuAttrValueEntity.getAttrId().toString());
                    groupAttrValueVo.setAttrName(spuAttrValueEntity.getAttrName());
                    groupAttrValueVo.setAttrValue(spuAttrValueEntity.getAttrValue());
                    return groupAttrValueVo;
                }).collect(Collectors.toList());
                allGroupAttrValuesVos.addAll(groupAttrValueVos);
            }
            if (!CollectionUtils.isEmpty(skuAttrValueEntities)) {
                List<GroupAttrValueVo> groupAttrValueVos = skuAttrValueEntities.stream().map(skuAttrValueEntity -> {
                    GroupAttrValueVo groupAttrValueVo = new GroupAttrValueVo();
                    groupAttrValueVo.setAttrId(skuAttrValueEntity.getAttrId().toString());
                    groupAttrValueVo.setAttrName(skuAttrValueEntity.getAttrName());
                    groupAttrValueVo.setAttrValue(skuAttrValueEntity.getAttrValue());
                    return groupAttrValueVo;
                }).collect(Collectors.toList());
                allGroupAttrValuesVos.addAll(groupAttrValueVos);
            }
            itemGroupVo.setAttrs(allGroupAttrValuesVos);
            return itemGroupVo;
        }).collect(Collectors.toList());
        //3、根据属性id 在spu和sku的 attrValues

        return itemGroupVos;
    }

}